<?php
namespace Admin\Controller;
use Common\Controller\CommonController;
use Common\Logic\ImageLogic;
class IndexController extends CommonController {
    public function _initialize(){
        parent::_initialize();
    }
    //空操作
    public function _empty(){
        header("HTTP/1.0 404 Not Found");
        $this->display('Public:404');
    }
    public function index(){
		$this->display();
    }

    /**
     * 统计全站信息
     */
    public function infoStatistics(){
    	//今日平均成交价格
       $wheretrade['over_time']=array('between',[strtotime(date("Y-m-d"),time()),time()]);
       $avgprice=M('wallet_trade')->where('trade_status = 2')->where($wheretrade)->avg('price');
       $data['avgprice']=$avgprice;
       $where['add_time']=array('between',[strtotime(date("Y-m-d"),time()),time()]);
       //今日动态奖金
       $dt_sum=M('dt_reward_log')->where($where)->sum('num');
       $data['dt_sum']=$dt_sum;
       //今日释放奖励钱包
       $jiangli_sf_sum=M('admin_coin_sf')->where($where)->sum('num');
       $data['jiangli_sf_num']=$jiangli_sf_sum;
       //今日添加冻结奖励钱包
       $jiangli_num=M('admin_coin_log')->where($where)->sum('num');
       $data['jiangli_num']=$jiangli_num;
       //今日转账总量
       $transfer_num=M('transfer_log')->where($where)->sum('num');
       $data['transfer_num']=$transfer_num;
       //持币生息总量
       $hold_num=M('hold_log')->where($where)->sum('num');
       $data['hold_num']=$hold_num;
       //会员可用非遗链总和
       $fyl_sum=M('wallet')->sum('fyl_num');
       $data['fyl_num']=$fyl_sum;
       //会员冻结非遗链总和
       $fyl_frozen_sum=M('wallet')->sum('fyl_frozen_num');
       $data['fyl_frozen_num']=$fyl_frozen_sum;
       //会员冻结奖励钱包总和
       $jiangli_num=M('wallet')->sum('jiangli_num');
       $data['jiangli_num']=$jiangli_num;
       //会员可用奖励钱包总和
       $jiangli_ky_num=M('wallet')->sum('jiangli_ky_num');
       $data['jiangli_ky_num']=$jiangli_ky_num;
       //会员动态奖励总和
       $fyl_dt_num=M('wallet')->sum('fyl_dt_num');
       $data['fyl_dt_num']=$fyl_dt_num;
       $this->ajaxReturn($data);
    }

    /**
     * 删除缓存方法
     */
    public function cache(){
        $cacheDir = $_POST['type'];
        $type = $cacheDir;
        //将传递过来的值进行切割，我是已“-”进行切割的
        $name = explode('-', $type);
        //得到切割的条数，便于下面循环
        $count = count($name);
        //循环调用上面的方法
        for ($i = 0; $i < $count; $i++)
        {
            //得到文件的绝对路径
            $abs_dir = dirname(dirname(dirname(dirname(__FILE__))));
            //组合路径
            $pa = $abs_dir . str_replace("/", "\\", str_replace("./", "\\", RUNTIME_PATH)); //得到运行时的目录
            $runtime = $pa . 'common~runtime.php';
            if (file_exists($runtime))//判断 文件是否存在
            {
                unlink($runtime); //进行文件删除
            }
            //调用删除文件夹下所有文件的方法
            $this->rmFile($pa, $name[$i]);
        }
        $data['status'] = 1;
        $data['info'] = "清理成功";
        $this->ajaxReturn($data);
    }

    /**
     * 删除文件和目录
     * @param type $path 要删除文件夹路径
     * @param type $fileName 要删除的目录名称
     */
    private function rmFile($path, $fileName)
    {//删除执行的方法
        //去除空格
        $path = preg_replace('/(\/){2,}|{\\\}{1,}/', '/', $path);
        //得到完整目录
        $path.= $fileName;
        //判断此文件是否为一个文件目录
        if (is_dir($path))
        {
            //打开文件
            if ($dh = opendir($path))
            {
                //遍历文件目录名称
                while (($file = readdir($dh)) != false)
                {
                    $sub_file_path = $path . "\\" . $file;
                    if ("." == $file || ".." == $file)
                    {
                        continue;
                    }
                    if (is_dir($sub_file_path))
                    {
                        $this->rmFile($sub_file_path, "");
                        rmdir($sub_file_path);
                    }
                    //逐一进行删除
                    unlink($sub_file_path);
                }
                //关闭文件
                closedir($dh);
            }
            rmdir($sub_file_path);//删除当前目录
        }
    }
    public function upload(){
    	$pic=I('pic');
    	$IMAGE=new ImageLogic();
    	$r=$IMAGE->uploadImage($pic);
    	if($r['status']==1){
    		$data['status']=1;
    		$data['url']=substr($r['url'],1);
    		$this->ajaxReturn($data);
    	}else{
    		$data['status']=0;
    		$data['info']='上传失败';
    		$this->ajaxReturn($data);
    	}
    }
}